package GUI;

import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Color;

import javax.swing.ImageIcon;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Calendar;

import logic.Member;
import logic.Message;
import javax.swing.UIManager;

public class SearchOrder extends JPanel{
	
private Client_GUI parent;
private JTextField textFieldMemberId;
private JTextField textFieldBookID;
private JTextField textFieldOrderDate;
private JLabel LBLMemberId;
private JLabel LBLCategory;
private JLabel LBLBookID;
private JLabel LBLOrderDate;
private JLabel lblYouMustGive;

private SearchOrder self;

	
	public SearchOrder(final Client_GUI parent) {
		this.setBackground(UIManager.getColor("inactiveCaption"));
		this.self = this;
		this.parent = parent;
		this.setSize(Client_GUI.MAIN_WIDTH, Client_GUI.MAIN_HEIGHT);
		setLayout(null);
		parent.safeToClear = false;
		
		JLabel lblSearchLoan = new JLabel("Search Order:");
		lblSearchLoan.setForeground(Color.RED);
		lblSearchLoan.setBounds(337, 11, 197, 52);
		lblSearchLoan.setFont(new Font("Tahoma", Font.BOLD, 18));
		add(lblSearchLoan);
		
		JLabel lblMemberId = new JLabel("Member Id:");
		lblMemberId.setFont(new Font("Tahoma", Font.BOLD, 13));
		lblMemberId.setBounds(125, 124, 131, 20);
		add(lblMemberId);
		
		textFieldMemberId = new JTextField();
		textFieldMemberId.setFont(new Font("Tahoma", Font.PLAIN, 13));
		textFieldMemberId.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent arg0) {
				lblYouMustGive.setVisible(false);
				parent.safeToClear =false ;
			}
		});
		textFieldMemberId.addFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent arg0) {
				ValidateData(false);
			}
		});
		textFieldMemberId.setColumns(10);
		textFieldMemberId.setBounds(309, 118, 197, 32);
		add(textFieldMemberId);
		
		LBLMemberId = new JLabel("");
		LBLMemberId.setBounds(514, 118, 30, 32);
		add(LBLMemberId);
		
		JLabel lblByMember = new JLabel("By Member:");
		lblByMember.setForeground(Color.RED);
		lblByMember.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblByMember.setBounds(125, 75, 115, 20);
		add(lblByMember);
		
		JLabel lblByBook = new JLabel("By Book:");
		lblByBook.setForeground(Color.RED);
		lblByBook.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblByBook.setBounds(125, 167, 131, 26);
		add(lblByBook);
		
		JLabel lblBookID = new JLabel("Book ID:");
		lblBookID.setFont(new Font("Tahoma", Font.BOLD, 13));
		lblBookID.setBounds(125, 198, 83, 20);
		add(lblBookID);
		
		textFieldBookID = new JTextField();
		textFieldBookID.setFont(new Font("Tahoma", Font.PLAIN, 13));
		textFieldBookID.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent arg0) {
				lblYouMustGive.setVisible(false);
				parent.safeToClear =false ;
			}
		});
		textFieldBookID.addFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent arg0) {
				ValidateData(false);
			}
		});
		textFieldBookID.setColumns(10);
		textFieldBookID.setBounds(309, 186, 197, 32);
		add(textFieldBookID);
		
		JLabel lblOrderDate = new JLabel("Order Date:");
		lblOrderDate.setFont(new Font("Tahoma", Font.BOLD, 13));
		lblOrderDate.setBounds(125, 248, 83, 20);
		add(lblOrderDate);
		
		textFieldOrderDate = new JTextField();
		textFieldOrderDate.setFont(new Font("Tahoma", Font.PLAIN, 13));
		textFieldOrderDate.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent arg0) {
				lblYouMustGive.setVisible(false);
				parent.safeToClear =false ;
			}
		});
		textFieldOrderDate.addFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent arg0) {
				ValidateData(false);
			}
		});
		textFieldOrderDate.setColumns(10);
		textFieldOrderDate.setBounds(309, 242, 197, 32);
		add(textFieldOrderDate);
		
		LBLBookID = new JLabel("");
		LBLBookID.setBounds(514, 186, 30, 32);
		add(LBLBookID);
		
		LBLOrderDate = new JLabel("");
		LBLOrderDate.setBounds(514, 242, 30, 32);
		add(LBLOrderDate);
		
		JButton btnSearch = new JButton("Search Order");
		btnSearch.setFont(new Font("Tahoma", Font.BOLD, 13));
		btnSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				ValidateData(true);
				
			}
		});
		btnSearch.setBounds(92, 423, 153, 40);
		add(btnSearch);
		
		JButton button = new JButton("Cancel");
		button.setFont(new Font("Tahoma", Font.BOLD, 13));
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				parent.RepaintMenu(parent.client.Privelege);				
			}
		});
		button.setBounds(565, 423, 153, 40);
		add(button);
		
		lblYouMustGive = new JLabel("You must give at least one  input!!!!");
		lblYouMustGive.setForeground(Color.RED);
		lblYouMustGive.setFont(new Font("Tahoma", Font.BOLD, 16));
		lblYouMustGive.setBounds(256, 380, 320, 32);
		add(lblYouMustGive);
		
		JButton btnNewButton = new JButton("Search Member");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				SearchMember topaint =new SearchMember(parent, SearchMember.SEARCH_FOR_SEARCH_ORDER, self);
				parent.safeToClear = true;
				parent.currentMain = topaint;
				parent.RepaintMain(topaint);
				
			}
		});
		btnNewButton.setFont(new Font("Tahoma", Font.BOLD, 12));
		btnNewButton.setBounds(565, 118, 137, 32);
		add(btnNewButton);
		
		JButton btnSearchBook = new JButton("Search Book");
		btnSearchBook.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				SearchBook topaint =new SearchBook(parent, SearchBook.SEARCH_FOR_SEARCH_ORDER, self);
				parent.safeToClear = true;
				parent.currentMain = topaint;
				parent.RepaintMain(topaint);
			}
		});
		btnSearchBook.setFont(new Font("Tahoma", Font.BOLD, 12));
		btnSearchBook.setBounds(565, 186, 137, 32);
		add(btnSearchBook);
		lblYouMustGive.setVisible(false);
		
		
		
		if(parent.client.Privelege == Member.MEMBER_PRIVILIGE){
			this.setEnabled(false);
			ArrayList<String> index = new ArrayList<String>();
			ArrayList<Object> data = new ArrayList<Object>();		
						
			index.add("UID");
			data.add(parent.client.GetUID());
			
			int actionOut = Message.SEARCH_ORDER_REQUEST;
	
			Message toSend = new Message(index, data, actionOut);
			
			if (parent.sendMessageToServer(toSend) == 1){//message sent successfully
				parent.SetLoading(true);
							
			}else{//message send failed
				//TODO verify extream situation
			}
		}
	
	}

	public void SetBID(String bid){
		this.textFieldBookID.setText(bid);
	}
	
	public void SetUID(String uid){
		this.textFieldMemberId.setText(uid);
	}

	private void ValidateData(boolean fromOK){
	
	ImageIcon okImageIcon = new ImageIcon(getClass().getClassLoader().getResource("resource/ok.png"));
	ImageIcon OK = new ImageIcon (okImageIcon.getImage().getScaledInstance(24, 24, java.awt.Image.SCALE_SMOOTH));
	
	ImageIcon badImageIcon = new ImageIcon(getClass().getClassLoader().getResource("resource/bad.png"));
	ImageIcon BAD = new ImageIcon (badImageIcon.getImage().getScaledInstance(24, 24, java.awt.Image.SCALE_SMOOTH));
	
	boolean valid = true;
	
	String temp; // the string to be tested on each test
	
	
	//validate Order date
	temp = textFieldOrderDate.getText();
	if(temp.length()== 10)
	try
	{
		if ((temp.charAt(4) == '-' && temp.charAt(7) == '-') || (temp.charAt(4) == '/' && temp.charAt(7) == '/') || (temp.charAt(4) == '.' && temp.charAt(7) == '.')) 
			LBLOrderDate.setIcon(OK);
		else {
			LBLOrderDate.setIcon(BAD);
			valid = false;
		}
		
		int year = Integer.parseInt(temp.substring(0, 4));
		int month = Integer.parseInt(temp.substring(5, 7));
		int day = Integer.parseInt(temp.substring(8, temp.length()));

		Calendar validator = Calendar.getInstance();
		validator.set(year, month-1, day);
		validator.setLenient(false);
		validator.getTime();
		
	}catch(NumberFormatException e){
		LBLOrderDate.setIcon(BAD);
		valid = false;
	}catch(Exception e){
		LBLOrderDate.setIcon(BAD);
		valid = false;
	}
	else if(!temp.equals("")) {
		LBLOrderDate.setIcon(BAD);
		valid = false;
	}
	
	
	

	//Validate UID
	temp = textFieldMemberId.getText();
	if (!temp.equals(""))
	try{
		char c = temp.charAt(0);
		if (c == '+' || c == '-' || temp.length()!=9) {
			LBLMemberId.setIcon(BAD);
			valid = false;
		}
		else LBLMemberId.setIcon(OK);
		
		
		Integer.parseInt(temp);	
	}catch(NumberFormatException e){
		LBLMemberId.setIcon(BAD);
		valid = false;
	}
	else LBLMemberId.setIcon(null);
	
	

	//Validate BOOK ID
	temp = textFieldBookID.getText();
	if (!temp.equals(""))
	try{
		char c = temp.charAt(0);
		if (c == '+' || c == '-') {
			LBLBookID.setIcon(BAD);
			valid = false;
		}
		else LBLBookID.setIcon(OK);

		
		Integer.parseInt(temp);	
	}catch(NumberFormatException e){
		LBLBookID.setIcon(BAD);
		valid = false;
	}
	else LBLBookID.setIcon(null);
	
	
	
	
	
	
	if (fromOK){
		if (textFieldBookID.getText().equals("")&& textFieldMemberId.getText().equals("")){
			lblYouMustGive.setVisible(true);
			valid = false;
		}
		
	}
	
	
	
	if (valid && fromOK){
		ArrayList<String> index = new ArrayList<String>();
		ArrayList<Object> data = new ArrayList<Object>();
		
		if(!textFieldMemberId.getText().equals(""))
		{
		index.add("UID");
		data.add(textFieldMemberId.getText());
		}
		if(!textFieldBookID.getText().equals(""))
		{
		index.add("BID");
		data.add(textFieldBookID.getText());
		}
		if(!textFieldOrderDate.getText().equals(""))
		{
		index.add("ORDER_DATE");
		data.add(textFieldOrderDate.getText());
		}
		
		
		
		
		
	
	
		int action = Message.SEARCH_ORDER_REQUEST;
		
		Message toSend = new Message(index, data, action);
		
		if (parent.sendMessageToServer(toSend) == 1){//message sent successfully
			parent.SetLoading(true);			
		}else{//message send failed
			//TODO verify extream situation
		}
		
	
}


}
}
